package weekMatch.one_102;

public class LeeCode_01 {
    public static void main(String[] args) {
        System.out.println(findTheLongestBalancedSubstring("01000111"));
        System.out.println(findTheLongestBalancedSubstring("000111"));
        System.out.println(findTheLongestBalancedSubstring("111"));
        System.out.println(findTheLongestBalancedSubstring("001"));
    }

    private static int findTheLongestBalancedSubstring(String s) {
        int len = s.length();
        int res = 0;
        int c0 = 0;
        int c1 = 0;
        int i = 0;
        while (i < len) {
            while (i < len && s.charAt(i) == '0') {
                c0++;
                i++;
            }
            while (i < len && s.charAt(i) == '1') {
                c1++;
                i++;
            }
            if (c1 >= c0) {
                res = Math.max(res, 2 * c0);
            }
            if (c0 >= c1){
                res = Math.max(res, c1 * 2);
            }
            c0 = 0;
            c1 = 0;
        }
        return res;
    }

}
